package com.yegs.jindie.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yegs.jindie.entity.DeliveryTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CategoryService extends BaseService {


    @Autowired
    private AuthService authService;

    private static final String URL = "https://api.kingdee.com/jdyscm/productCategory/list?";


    protected String getUrl(){
        String request_url = URL + "access_token="+ authService.getAccessToken()
                +"&dbId="+basisTO.getAppId()
                +"&pageSize=500";
        return request_url;
    }

    public List<Integer> getCategory() throws Exception{
        List<Integer> list = new ArrayList<>();
        String post =  super.get(this.getUrl());
        JSONObject jsonObject = JSONObject.parseObject(post);
        if(!jsonObject.containsKey("items")){
            return list;
        }
        JSONArray items = jsonObject.getJSONArray("items");
        for(int i = 0; i < items.size(); i ++){
            JSONObject jo = items.getJSONObject(i);
            String name = jo.getString("name");
            if(this.checkName(name)){
                Integer id = jo.getInteger("id");
                list.add(id);
            }
        }
        return list;
    }
    private boolean checkName(String name){
        return
                name.equals("电脑")||
                name.equals("平板电脑")||
                name.equals("手机");
    }
}
